{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bayesian Statistics\n",
    "\n",
    "Bayes rule\n",
    "$$\n",
    "p(\\theta| D) = \\frac{p(D|\\theta)p(\\theta)}{p(D)}\n",
    "$$\n",
    "However, in the samples that we will soon see it is far more convenient to write the above as\n",
    "$$\n",
    "p(\\theta| D) \\propto p(D|\\theta)p(\\theta)\n",
    "$$\n",
    "since $p(D)$ is a constant **that does not depend on $\\theta$**.\n",
    "\n",
    "However, we Bayesian analysis is far more involved in solving the following integral:\n",
    "$$\n",
    "p(D^*|D) = \\int p(D^*|\\theta)p(\\theta|D) d\\theta\n",
    "$$\n",
    "We denote $D^*$ to be test data, $D$ to be the train set and $\\theta$ to be a hidden parameter. This can often be thought of as doing model averaging if $p(\\theta|D)$ is thought of as a weighting function.\n",
    "\n",
    "## Coin Tossing Example\n",
    "\n",
    "Suppose we have a biased coin where the true probability of landing heads is 0.6. We assume however, (for some odd reason that it is biased towards landing tails). A suitable prior for this would be a beta distribution which has the following functional form:\n",
    "$$\n",
    "p(\\theta) = \\frac{\\Gamma(\\alpha)\\Gamma(\\beta)}{\\Gamma(\\alpha + \\beta)}\\theta^{\\alpha - 1}(1-\\theta)^{\\beta - 1}\n",
    "$$\n",
    "and the distribution looks like (https://en.wikipedia.org/wiki/Beta_distribution):\n",
    "![beta](./images/Beta_distribution_pdf.svg.png)\n",
    "\n",
    "Since we believe it is left skewed we will choose $\\alpha < \\beta$. Let $\\alpha = 2$, $\\beta = 3$ and hence, **this forms our prior distribution**. Assume that after 10 coin tosses we observe 7 heads and 3 tails. Each (individual) coin toss is distributed as a bernoulli distribution, conditional on $\\theta$, the probability of achieving a heads. At this point a frequentist method (I don't mean this in a derogative fashion) would state that the probability of receiving a heads is 0.7.\n",
    "\n",
    "The likelihood of the data can be states as follows:\n",
    "$$ p(y_1, \\cdots, y_{10}| \\theta) = \\prod_{i=1}^{10} p(y_i|\\theta)$$ since each draw is independent **given $\\theta$** where, $p(y_i|\\theta) = \\theta^{1(y_i=1)}(1-\\theta)^{1(y_i=0)}$. Hence in our example, the likelihood $ p(y_1, \\cdots, y_{10}| \\theta) = \\theta^7(1-\\theta)^3$. **Keep in mind that $\\theta$ is considered fixed as far as the likelihood is concerned**, hence it is a bernoullii distribution, **not a beta distribution**.\n",
    "\n",
    "### The posterior\n",
    "\\begin{align}\n",
    "p(\\theta | y) \\propto & \\quad p(y|\\theta) p(\\theta) \\\\\n",
    "\\propto & \\quad \\theta^7(1-\\theta)^3 \\theta^2(1-\\theta)^3 = \\theta^{9}(1-\\theta)^6\n",
    "\\end{align}\n",
    "The normalising constant is irrelevant as we can see that the posterior $p(\\theta | y)$ is a beta distribution simply by looking at its functional form. In fact the normalising constant (also known as the partition function) is simply,\n",
    "$$\\int_0^1 \\theta^{9}(1-\\theta)^6 d\\theta$$ since all probability distributions integrate out to one. In this case this turns out to be $\\frac{\\Gamma(9)\\Gamma(6)}{\\Gamma(15)}$. What is far more important to recognise than the normalising constant is that $\\theta \\sim Be(9, 6)$.\n",
    "\n",
    "### The probability of $p(y^*=1|y)$\n",
    "\\begin{align}\n",
    "p(y^*=1|y) &= \\int p(y^*=1|\\theta)p(\\theta|y) d\\theta\n",
    "\\end{align}\n",
    "\n",
    "A frequentist method would have chosen the Maximum-A-Posteriori (MAP) estimate and plugged into $\\theta$ without integrating it out. \n",
    "$$\n",
    "\\begin{align}\n",
    "p(y^*=1|y) &= \\int p(y^*=1|\\theta)p(\\theta|y) d\\theta \\\\\n",
    "&= \\int \\theta \\frac{\\Gamma(15)}{\\Gamma(9)\\Gamma(6)} \\theta^{9}(1-\\theta)^6 d\\theta\\\\\n",
    "&= \\frac{\\Gamma(15)}{\\Gamma(9)\\Gamma(6)} \\int\\theta^{10}(1-\\theta)^6 d\\theta\\\\\n",
    "&= \\frac{\\Gamma(15)}{\\Gamma(9)\\Gamma(6)} \\frac{\\Gamma(10)\\Gamma(6)}{\\Gamma(16)} \\int\\frac{\\Gamma(16)}{\\Gamma(10)\\Gamma(6)}\\theta^{10}(1-\\theta)^6 d\\theta \\\\\n",
    "&= \\frac{\\Gamma(15)}{\\Gamma(9)\\Gamma(6)} \\frac{\\Gamma(10)\\Gamma(6)}{\\Gamma(16)}  \\quad 1 \\\\\n",
    "&= \\frac{9}{15}\n",
    "\\end{align}\n",
    "$$\n",
    "Note the rule $\\Gamma(n) = (n-1)\\Gamma(n-1)$ was used."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import pymc3 as pm\n",
    "\n",
    "from scipy.special import gamma\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Auto-assigning NUTS sampler...\n",
      "Initializing NUTS using ADVI...\n",
      "Average Loss = 8.1227:   1%|          | 1292/200000 [00:00<00:15, 12915.73it/s]\n",
      "Convergence archived at 2500\n",
      "Interrupted at 2,500 [1%]: Average Loss = 7.9508\n",
      "100%|██████████| 100500/100500 [00:37<00:00, 2691.01it/s]\n"
     ]
    }
   ],
   "source": [
    "alpha = 2\n",
    "beta = 3\n",
    "y_obs = np.zeros(10)\n",
    "y_obs[:7] = 1\n",
    "niter = 10000\n",
    "\n",
    "with pm.Model() as model:\n",
    "    θ = pm.Beta('θ', alpha=alpha, beta=beta)\n",
    "    y = pm.Bernoulli('y', p=θ, observed=y_obs)\n",
    "    \n",
    "    trace = pm.sample(niter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# norm_const = gamma(9)*gamma(6)/gamma(15)\n",
    "theta = np.linspace(0,1,1000)\n",
    "post_theta = (theta**9)*(1-theta)**6\n",
    "C = np.sum(post_theta*(theta[1]-theta[0]))\n",
    "post_theta = post_theta/C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3Xd8VHW+//HXZyYJSFcJiIBE6SUR\nMKKgwQIqYtu1rOWu5a67yOre9W67F9e9rust692irqs/V1xde8VCL4poolKM9CqRGkENLYC0zDmf\n3x8zcEMIZJLMzHfK5/l4zIPJnJMz70OST775nu/5fkVVMcYYk14CrgMYY4yJPSvuxhiThqy4G2NM\nGrLibowxaciKuzHGpCEr7sYYk4asuBtjTBqy4m6MMWnIirsxxqShLFdv3LZtW83Ly3P19sYYk5I+\n++yzLaqaW9d+zop7Xl4epaWlrt7eGGNSkoisj2Y/65Yxxpg0ZMXdGGPSkBV3Y4xJQ1bcjTEmDVlx\nN8aYNGTF3Rhj0pAVd2OMSUNW3I1JFr4PXsh1CpMmnN3EZIwBNsyBBS/A2hLYsSH8WquOkHcuDLwZ\nupwDIm4zmpRkxd0YF7atgaljYPV0yGkJXS+Agu8BAlvL4PNpsPhVyCuCyx+Btt1cJzYpxoq7MYm2\nfAK8c2f4+UUPwJk/hJzmh+9TtRfmvwCz/gueHApXPwm9r0h8VpOyrM/dmESa8zd4/WbI7QF3zYFz\n7j6ysANkHwdnjYI750C7XvDa92HeU4nPa1KWFXdjEmXOEzDt36HX5fDPU6F1p7o/p9XJcNsU6DkS\npvwSPnsu/jlNWrDibkwirJgE0+4JF/brnoWsJtF/bnbT8Od0Gw6TfgZrPohTSJNOrLgbE2+bF8Nb\nP4KOA+Gav0Mwu/7HyGoSLvBte8Abt8H2qGZ9NRnMirsx8bR/d7gYN20DN7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2OdO4G+R5zcAr7nOnYBzvgBoFnn+40w458h+LYFiYA5Q6Dp3Ar7O3YEFwPGRj9u5zp2A\ncx4L/DjyvA+wznXuRp7zUGAgsPQo20cCUwnfJ3Q2MDeW75/MLfdBQJmqrlHVA8CrwFU19rkKeC7y\nfBwwTERSeeLmOs9ZVWep6p7Ih3MI33eQyqL5OgP8J/AHYF8iw8VJNOf8I+BxVd0OoKrfJDhjrEVz\nzgq0ijxvzZH306QUVS2mlvt9qrkKeF7D5gBtRKRDrN4/mYt7bdMe1Fwi6LBpD4CD0x6kqmjOubrb\nCf/mT2V1nrOIDAA6q+qkRAaLo2i+zj2AHiLysYjMiczMmsqiOef7ge+LSDnh0Xn/kphoztT3571e\nknlCkphNe5BCoj4fEfk+UAicF9dE8XfMcxaRAPAwcFuiAiVANF/nLMJdM+cT/uusRET6qeqOOGeL\nl2jO+UbgWVX9c2TCwhci5+zHP54Tca1fydxyr8+0Bxxr2oMUEs05IyLDgXuBK1V1f4KyxUtd59wS\n6Ad8ICLrCPdNTkjxi6rRfm+PV9UqVV0LrCJc7FNVNOd8O/A6gKrOBpoSnncmXUX1895QyVzcM3Ha\ngzrPOdJF8SThwp7q/bBQxzmraqWqtlXVPFXNI3yd4UpVTbL5ouslmu/tdwhfPEdE2hLuplmT0JSx\nFc05bwCGAYhIb8LFvSKhKRNrAnBLZNTM2UClqm6O2dFdX1Gu42rzSMILhXwB3Bt57QHCP9wQ/uK/\nAZQB84DTXGdOwDm/B3wNLIw8JrjOHO9zrrHvB6T4aJkov84CPAQsB5YAN7jOnIBz7gN8THgkzULg\nYteZG3m+rwCbgSrCrfTbgdHA6Gpf48cj/x9LYv19bXeoGmNMGkrmbhljjDENZMXdGGPSkBV3Y4xJ\nQ1bcjTEmDVlxN8aYNGTF3Rhj0pAVd2OMSUNW3I0xJg39f+C0Imkii5ElAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x123497e10>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.hist(trace['θ'][1000:],100, normed='true')\n",
    "plt.plot(theta, post_theta)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\\int \\theta p(\\theta | y) d\\theta \\approx \\sum_{\\theta\\sim p(\\theta|y)} \\theta$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.59970305353900688"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(trace['θ'][1000:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "latex_envs": {
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 0
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
